import { submitDealReq } from './auth'
import request from '@/utils/request'
import { getRemoteUrlParam } from '@/utils/hyurl'

export function btnUploadFile(thisObj, btnItem, data) {
  thisObj.nowBtnItem = btnItem
  const formData = new FormData()
  formData.append('file', data.file)
  for (const key in thisObj.formModel) {
    if (thisObj.formModel[key] !== undefined) {
      formData.append(key, thisObj.formModel[key])
    }
  }
  for (const key in thisObj.openParam) {
    if (thisObj.openParam[key] !== undefined) {
      formData.append(key, thisObj.openParam[key])
    }
  }
  // openParam 中必须包含pkRefId（主业务主键）,pkSubId(次业务主键,单业务可无)
  const clickBtn = Object.assign({}, thisObj.uiPageUrl)
  if (btnItem !== undefined) {
    clickBtn.S_UI_BTN_IDX_2025 = btnItem.id
    clickBtn.S_UI_NAME_IDX_2025 = btnItem.name
    clickBtn.S_UI_FN_TYPE_2025 = btnItem.fnType
  }
  formData.append('S_UI_CLICK_BTN_2025', JSON.stringify(clickBtn))
  request.upload('/autodeal/file/ctr/upload', formData).then((res) => {
    if (res.status === 200) {
      const fileList = res.data.fileList
      for (const i in fileList) {
        thisObj.$message.success(`${fileList[i].name} 上传文件成功.`)
        if (btnItem.fnType === 5) {
          // 导入类
          btnImportData(thisObj, btnItem, fileList[i])
        }
      }
    } else {
      thisObj.$message.error(res.data.message)
    }
  })
}

/**
 * 按钮导入文件数据
 * @param thisObj
 * @param btnItem
 * @param fileData
 */
export function btnImportData(thisObj, btnItem, fileData) {
  const impportTmpData = {}
  impportTmpData.S_PAGE_ID = btnItem.refImport.refImportObjPageId
  impportTmpData.S_PAGE_VER = btnItem.refImport.refImportObjPageVer
  impportTmpData.S_PARTITION = btnItem.refImport.refImportObjPartition

  const importTmpParam = {}
  importTmpParam.S_DY_PARAM = btnItem.refImport.refImportObjUrlParam
  const newUrlKey = 'import.' + btnItem.refImportUrlKey
  thisObj.dealApi[newUrlKey] = impportTmpData
  thisObj.dealParam[newUrlKey] = importTmpParam
  const postParam = getRemoteUrlParam(thisObj, newUrlKey, thisObj.globalData)
  postParam.ID = fileData.uid
  postParam.I_IMPORT_OBJ = btnItem.refImport.refImportType
  const clickBtn = Object.assign({}, thisObj.uiPageUrl)
  if (btnItem !== undefined) {
    clickBtn.btnIdIdx = btnItem.id
    clickBtn.btnNameIdx = btnItem.name
    clickBtn.S_UI_BTN_IDX_2025 =btnItem.id
    clickBtn.S_UI_NAME_IDX_2025 = btnItem.name
    clickBtn.S_UI_FN_TYPE_2025 = btnItem.fnType
  }
  postParam.S_UI_CLICK_BTN_2025 = clickBtn
  submitDealReq(postParam).then((res) => {
    if (res.data.success) {
      thisObj.$message.success(`${fileData.name} 上传数据成功.`)
      thisObj.searchQuery()
    } else {
      thisObj.$message.warning(res.data.message)
    }
  }).catch(reason => {
    thisObj.$message.error(`${fileData.name} 上传数据失败.`)
    console.log('reason', reason)
  })
}

/**
 * 按钮导入文件数据
 * @param thisObj
 * @param btnItem
 */
export function btnImport(thisObj, btnItem) {
  const impportTmpData = {}
  impportTmpData.S_PAGE_ID = btnItem.refImport.refImportObjPageId
  impportTmpData.S_PAGE_VER = btnItem.refImport.refImportObjPageVer
  impportTmpData.S_PARTITION = btnItem.refImport.refImportObjPartition

  const importTmpParam = {}
  importTmpParam.S_DY_PARAM = btnItem.refImport.refImportObjUrlParam
  const newUrlKey = 'import.' + btnItem.refImportUrlKey
  thisObj.dealApi[newUrlKey] = impportTmpData
  thisObj.dealParam[newUrlKey] = importTmpParam
  const postParam = getRemoteUrlParam(thisObj, newUrlKey, thisObj.globalData)
  const refImport = btnItem.refImport
  // postParam.I_IMPORT_OBJ = refImport.refImportType
  postParam.refImportUrlKey = refImport.refImportUrlKey
  postParam.refImportPkId = refImport.refImportPkId
  postParam.viewKey = refImport.viewKey
  submitDealReq(postParam).then((res) => {
    console.log('res....', res)
    if (res.data.success) {
      thisObj.searchQuery()
    } else {
      thisObj.$message.warning(res.data.message)
    }
  }).catch(reason => {
    console.log('reason', reason)
  })
}
